/**********************************************************************\

 Copyright 2012 Diggory Blake

 This file is part of klip-engine

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

\**********************************************************************/

#include "kimagecache.h"
#include "kgraphics.h"
#include "kimage.h"

kImageCache::~kImageCache() {
	clear();
}
kImage* kImageCache::get(std::function<bool (const kVec2i&, keImageOptions)> isCompatible) {
	for (auto i = _images.begin(); i != _images.end(); i++) {
		kImage* image = *i;
		if (isCompatible(image->getSize(), image->getOptions())) {
			_images.erase(i);
			return image;
		}
	}

	return 0;
}
void kImageCache::release(kImage* image) {
	while (_images.size() >= _cacheSize) {
		_images.pop_back();
	}
	_images.push_front(image);
}
void kImageCache::clear() {
	for (auto i = _images.begin(); i != _images.end(); i++)
		_graphics->deleteImage(*i);
	_images.clear();
}
